package org.grow.pointcolor.widget;

import org.grow.pointcolor.tool.FormatTool;
import org.grow.pointcolor.tool.ScanTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.ExecutorService;

@Component()
public class TotalFrame extends JFrame {
    public TotalFrame() throws HeadlessException {

    }

    @Autowired
    private JTextArea console;
    @Autowired
    private JButton startButton;
    @Autowired
    private JButton clearButton;
    @Autowired
    private ScanTask scanTask;
    @Autowired
    private ExecutorService es;

    @Autowired
    private JFormattedTextField xTextField;
    @Autowired
    private JFormattedTextField yTextField;
    @Autowired
    private JFormattedTextField outTextField;
    @Autowired
    private JButton detectiveButton;

    private JPanel jPanel1;
    private JPanel jPanel2;

    @PostConstruct
    public void springInit() {

        this.setTitle("显示鼠标的位置与颜色");
        this.setSize(600, 400);
        this.setLayout(new FlowLayout());

        Dimension size = console.getPreferredScrollableViewportSize();
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(110, 90, size.width, size.height);
        scrollPane.setViewportView(console);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        jPanel1 = new JPanel();
        jPanel1.setSize(400, 300);
        jPanel1.setLayout(new FlowLayout());
        jPanel1.add(startButton);
        jPanel1.add(clearButton);
        jPanel1.add(scrollPane);
        this.add(jPanel1);

        jPanel2 = new JPanel();
        jPanel2.setSize(400, 50);
        jPanel2.setBackground(Color.cyan);
        jPanel2.setLayout(new FlowLayout());
        jPanel2.add(xTextField);
        jPanel2.add(yTextField);
        jPanel2.add(detectiveButton);
        jPanel2.add(outTextField);
        this.add(jPanel2);

        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                //todo: shutdown es
                scanTask.getLoopkey().set(false);
                es.shutdownNow();
            }
        });
//        KeyboardFocusManager m = KeyboardFocusManager.getCurrentKeyboardFocusManager();
//        m.addKeyEventPostProcessor((KeyEvent e)->{
//
//            this.keyTyped(e);
//            return false;
//        });
    }

    @Autowired
    private Robot robot;

//    public void keyTyped(KeyEvent e) {
//        System.out.println(Thread.currentThread().getName()+" 监听键盘事件 "+e.getKeyChar() +" 源的类型 "+e.getSource().getClass());
//
//        if (e.getKeyChar() == 'k') {
//            Point location = MouseInfo.getPointerInfo().getLocation();
//            xTextField.setText(location.x + "");
//            yTextField.setText(location.y + "");
//            Color c = robot.getPixelColor(location.x, location.y);
//            String s = FormatTool.pointColorFormat(location.x, location.y, c);
//            outTextField.setText(s);
//            console.append(s);
//            outTextField.setBackground(c);
//        }
//
//    }


}
